# 放公用的工具函数


from itsdangerous import TimedJSONWebSignatureSerializer as TJWSS
from django.conf import settings
# 加密openid的函数
def encode_openid(openid):
    # 生成加密器
    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=300)

    # 加密数据
    encode_data = s.dumps({"openid":openid})

    return encode_data.decode()



# 解密openid的函数
def decode_openid(encode_data):
    # 生成加密器
    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=300)

    # 解密数据
    try:
        decode_data = s.loads(encode_data)
    except Exception as e:
        print(e)
        return None

    openid = decode_data.get('openid')
    return openid